package com.talent.jiwen.util;

import android.os.Environment;

import java.io.File;

public class FileUtils {

    public static  final String BaseFilePath= Environment.getExternalStorageDirectory().getPath();
    public static  final String BASE_PATH=BaseFilePath+"/JiWen/";

    public static  final String COURSE_PATH = BASE_PATH+"course/";


    /**
     * 创建文件夹
     *
     * @param dir
     * @return
     */
    public static boolean createDir(File dir) {
        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**截取视频文件名
     *
     * @param videoUrl
     * @return
     */
    public static String getVideoFileName(String videoUrl){

        File parentFile = new File(FileUtils.COURSE_PATH);

        if (!parentFile.exists()) {
            FileUtils.createDir(new File(FileUtils.COURSE_PATH));
        }

        String[] urlSplits = videoUrl.split("/");
        String fileName = urlSplits[urlSplits.length - 1];

        return FileUtils.COURSE_PATH  + fileName;
    }

    /**文件是否存在
     *
     * @param pathName
     * @return
     */
    public static boolean fileExist(String pathName){
        File file = new File(pathName);
        if (file.exists() && file.length() > 0) {//在本地存在
            return true;
        } else {
            return false;
        }
    }
}
